home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 1.toast / pc / sample code / files / morefiles / cheaders / directorycopy.h next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  21.3 KB  |  510 lines

  1. /*
  2. **    Apple Macintosh Developer Technical Support
  3. **
  4. **    DirectoryCopy: A robust, general purpose directory copy routine.
  5. **
  6. **    by Jim Luther, Apple Developer Technical Support Emeritus
  7. **
  8. **    File:        DirectoryCopy.h
  9. **
  10. **    Copyright © 1992-1999 Apple Computer, Inc.
  11. **    All rights reserved.
  12. **
  13. **    You may incorporate this sample code into your applications without
  14. **    restriction, though the sample code has been provided "AS IS" and the
  15. **    responsibility for its operation is 100% yours.  However, what you are
  16. **    not permitted to do is to redistribute the source as "DSC Sample Code"
  17. **    after having made changes. If you're going to re-distribute the source,
  18. **    we require that you make it clear in the source that the code was
  19. **    descended from Apple Sample Code, but that you've made changes.
  20. */
  21.  
  22. #ifndef __DIRECTORYCOPY__
  23. #define __DIRECTORYCOPY__
  24.  
  25. #include <Types.h>
  26. #include <Files.h>
  27.  
  28. #include "Optimization.h"
  29.  
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33.  
  34. /*****************************************************************************/
  35.  
  36. enum
  37. {
  38.     getNextItemOp            = 1,    /* couldn't access items in this directory - no access privileges */
  39.     copyDirCommentOp        = 2,    /* couldn't copy directory's Finder comment */
  40.     copyDirAccessPrivsOp    = 3,    /* couldn't copy directory's AFP access privileges */
  41.     copyDirFMAttributesOp    = 4,    /* couldn't copy directory's File Manager attributes */
  42.     dirCreateOp                = 5,    /* couldn't create destination directory */
  43.     fileCopyOp                = 6        /* couldn't copy file */
  44. };
  45.  
  46. /*****************************************************************************/
  47.  
  48. typedef    pascal    Boolean    (*CopyErrProcPtr) (OSErr error,
  49.                                            short failedOperation,
  50.                                            short srcVRefNum,
  51.                                            long srcDirID,
  52.                                            ConstStr255Param srcName,
  53.                                            short dstVRefNum,
  54.                                            long dstDirID,
  55.                                            ConstStr255Param dstName);
  56. /*    ¶ Prototype for the CopyErrProc function DirectoryCopy calls.
  57.     This is the prototype for the CopyErrProc function DirectoryCopy
  58.     calls if an error condition is detected sometime during the copy.  If
  59.     CopyErrProc returns false, then DirectoryCopy attempts to continue with
  60.     the directory copy operation.  If CopyErrProc returns true, then
  61.     DirectoryCopy stops the directory copy operation.
  62.  
  63.     error            input:    The error result code that caused CopyErrProc to
  64.                             be called.
  65.     failedOperation    input:    The operation that returned an error to
  66.                             DirectoryCopy.
  67.     srcVRefNum        input:    Source volume specification.
  68.     srcDirID        input:    Source directory ID.
  69.     srcName            input:    Source file or directory name, or nil if
  70.                             srcDirID specifies the directory.
  71.     dstVRefNum        input:    Destination volume specification.
  72.     dstDirID        input:    Destination directory ID.
  73.     dstName            input:    Destination file or directory name, or nil if
  74.                             dstDirID specifies the directory.
  75.  
  76.     __________
  77.     
  78.     Also see:    FilteredDirectoryCopy, FSpFilteredDirectoryCopy, DirectoryCopy, FSpDirectoryCopy
  79. */
  80.  
  81. #define CallCopyErrProc(userRoutine, error, failedOperation, srcVRefNum, srcDirID, srcName, dstVRefNum, dstDirID, dstName) \
  82.         (*(userRoutine))((error), (failedOperation), (srcVRefNum), (srcDirID), (srcName), (dstVRefNum), (dstDirID), (dstName))
  83.  
  84. /*****************************************************************************/
  85.  
  86. typedef    pascal    Boolean    (*CopyFilterProcPtr) (const CInfoPBRec * const cpbPtr);
  87.  
  88. /*    ¶ Prototype for the CopyFilterProc function.
  89.     This is the prototype for the CopyFilterProc function called by
  90.     FilteredDirectoryCopy and GetLevelSize. If true is returned,
  91.     the file/folder is included in the copy, otherwise it is excluded.
  92.     
  93.     pb    input:    Points to the CInfoPBRec for the item under consideration.
  94.  
  95.     __________
  96.     
  97.     Also see:    FilteredDirectoryCopy, FSpFilteredDirectoryCopy
  98. */
  99.  
  100. #define CallCopyFilterProc(userRoutine, cpbPtr) (*(userRoutine))((cpbPtr))
  101.  
  102. /*****************************************************************************/
  103.  
  104. pascal    OSErr    FilteredDirectoryCopy(short srcVRefNum,
  105.                                       long srcDirID,
  106.                                       ConstStr255Param srcName,
  107.                                       short dstVRefNum,
  108.                                       long dstDirID,
  109.                                       ConstStr255Param dstName,
  110.                                       ConstStr255Param copyName,
  111.                                       void *copyBufferPtr,
  112.                                       long copyBufferSize,
  113.                                       Boolean preflight,
  114.                                       CopyErrProcPtr copyErrHandler,
  115.                                       CopyFilterProcPtr copyFilterProc);
  116. /*    ¶ Make a copy of a directory structure in a new location with item filtering.
  117.     The FilteredDirectoryCopy function makes a copy of a directory
  118.     structure in a new location. If copyBufferPtr <> NIL, it points to
  119.     a buffer of copyBufferSize that is used to copy files data. The
  120.     larger the supplied buffer, the faster the copy. If
  121.     copyBufferPtr = NIL, then this routine allocates a buffer in the
  122.     application heap. If you pass a copy buffer to this routine, make
  123.     its size a multiple of 512 ($200) bytes for optimum performance.
  124.     
  125.     The optional copyFilterProc parameter lets a routine you define
  126.     decide what files or directories are copied to the destination.
  127.     
  128.     FilteredDirectoryCopy normally creates a new directory *in* the
  129.     specified destination directory and copies the source directory's
  130.     content into the new directory. However, if root parent directory
  131.     (fsRtParID) is passed as the dstDirID parameter and NULL is
  132.     passed as the dstName parameter, DirectoryCopy renames the
  133.     destination volume to the source directory's name (truncating
  134.     if the name is longer than 27 characters) and copies the source
  135.     directory's content into the destination volume's root directory.
  136.     This special case is supported by FilteredDirectoryCopy, but
  137.     not by FSpFilteredDirectoryCopy since with FSpFilteredDirectoryCopy,
  138.     the dstName parameter can not be NULL.
  139.     
  140.     srcVRefNum        input:    Source volume specification.
  141.     srcDirID        input:    Source directory ID.
  142.     srcName            input:    Source directory name, or nil if
  143.                             srcDirID specifies the directory.
  144.     dstVRefNum        input:    Destination volume specification.
  145.     dstDirID        input:    Destination directory ID.
  146.     dstName            input:    Destination directory name, or nil if
  147.                             dstDirID specifies the directory.
  148.     copyName        input:    Points to the new directory name if the directory
  149.                             is to be renamed or nil if the directory isn't to
  150.                             be renamed.
  151.     copyBufferPtr    input:    Points to a buffer of copyBufferSize that
  152.                             is used the i/o buffer for the copy or
  153.                             nil if you want DirectoryCopy to allocate its
  154.                             own buffer in the application heap.
  155.     copyBufferSize    input:    The size of the buffer pointed to
  156.                             by copyBufferPtr.
  157.     preflight        input:    If true, DirectoryCopy makes sure there are
  158.                             enough allocation blocks on the destination
  159.                             volume to hold the directory's files before
  160.                             starting the copy.
  161.     copyErrHandler    input:    A pointer to the routine you want called if an
  162.                             error condition is detected during the copy, or
  163.                             nil if you don't want to handle error conditions.
  164.                             If you don't handle error conditions, the first
  165.                             error will cause the copy to quit and
  166.                             DirectoryCopy will return the error.
  167.                             Error handling is recommended...
  168.     copyFilterProc    input:    A pointer to the filter routine you want called
  169.                             for each item in the source directory, or NULL
  170.                             if you don't want to filter.
  171.     
  172.     Result Codes
  173.         noErr                0        No error
  174.         readErr                –19        Driver does not respond to read requests
  175.         writErr                –20        Driver does not respond to write requests
  176.         badUnitErr            –21        Driver reference number does not
  177.                                     match unit table
  178.         unitEmptyErr        –22        Driver reference number specifies a
  179.                                     nil handle in unit table
  180.         abortErr            –27        Request aborted by KillIO
  181.         notOpenErr            –28        Driver not open
  182.         dskFulErr            -34        Destination volume is full
  183.         nsvErr                -35        No such volume
  184.         ioErr                -36        I/O error
  185.         bdNamErr            -37        Bad filename
  186.         tmfoErr                -42        Too many files open
  187.         fnfErr                -43        Source file not found, or destination
  188.                                     directory does not exist
  189.         wPrErr                -44        Volume locked by hardware
  190.         fLckdErr            -45        File is locked
  191.         vLckdErr             -46        Destination volume is read-only
  192.         fBsyErr                 -47        The source or destination file could
  193.                                     not be opened with the correct access
  194.                                     modes
  195.         dupFNErr            -48        Destination file already exists
  196.         opWrErr                -49        File already open for writing
  197.         paramErr            -50        No default volume or function not
  198.                                     supported by volume
  199.         permErr                 -54        File is already open and cannot be opened using specified deny modes
  200.         memFullErr            -108    Copy buffer could not be allocated
  201.         dirNFErr            -120    Directory not found or incomplete pathname
  202.         wrgVolTypErr        -123    Function not supported by volume
  203.         afpAccessDenied        -5000    User does not have the correct access
  204.         afpDenyConflict        -5006    The source or destination file could
  205.                                     not be opened with the correct access
  206.                                     modes
  207.         afpObjectTypeErr    -5025    Source is a directory, directory not found
  208.                                     or incomplete pathname
  209.     
  210.     __________
  211.     
  212.     Also see:    CopyErrProcPtr, CopyFilterProcPtr, FSpFilteredDirectoryCopy,
  213.                 DirectoryCopy, FSpDirectoryCopy, FileCopy, FSpFileCopy
  214. */
  215.  
  216. /*****************************************************************************/
  217.  
  218. pascal    OSErr    FSpFilteredDirectoryCopy(const FSSpec *srcSpec,
  219.                                          const FSSpec *dstSpec,
  220.                                          ConstStr255Param copyName,
  221.                                          void *copyBufferPtr,
  222.                                          long copyBufferSize,
  223.                                          Boolean preflight,
  224.                                          CopyErrProcPtr copyErrHandler,
  225.                                          CopyFilterProcPtr copyFilterProc);
  226. /*    ¶ Make a copy of a directory structure in a new location with item filtering.
  227.     The FSpFilteredDirectoryCopy function makes a copy of a directory
  228.     structure in a new location. If copyBufferPtr <> NIL, it points to
  229.     a buffer of copyBufferSize that is used to copy files data. The
  230.     larger the supplied buffer, the faster the copy. If
  231.     copyBufferPtr = NIL, then this routine allocates a buffer in the
  232.     application heap. If you pass a copy buffer to this routine, make
  233.     its size a multiple of 512 ($200) bytes for optimum performance.
  234.     
  235.     The optional copyFilterProc parameter lets a routine you define
  236.     decide what files or directories are copied to the destination.
  237.     
  238.     srcSpec            input:    An FSSpec record specifying the directory to copy.
  239.     dstSpec            input:    An FSSpec record specifying destination directory
  240.                             of the copy.
  241.     copyName        input:    Points to the new directory name if the directory
  242.                             is to be renamed or nil if the directory isn't to
  243.                             be renamed.
  244.     copyBufferPtr    input:    Points to a buffer of copyBufferSize that
  245.                             is used the i/o buffer for the copy or
  246.                             nil if you want DirectoryCopy to allocate its
  247.                             own buffer in the application heap.
  248.     copyBufferSize    input:    The size of the buffer pointed to
  249.                             by copyBufferPtr.
  250.     preflight        input:    If true, FSpDirectoryCopy makes sure there are
  251.                             enough allocation blocks on the destination
  252.                             volume to hold the directory's files before
  253.                             starting the copy.
  254.     copyErrHandler    input:    A pointer to the routine you want called if an
  255.                             error condition is detected during the copy, or
  256.                             nil if you don't want to handle error conditions.
  257.                             If you don't handle error conditions, the first
  258.                             error will cause the copy to quit and
  259.                             DirectoryCopy will return the error.
  260.                             Error handling is recommended...
  261.     copyFilterProc    input:    A pointer to the filter routine you want called
  262.                             for each item in the source directory, or NULL
  263.                             if you don't want to filter.
  264.     
  265.     Result Codes
  266.         noErr                0        No error
  267.         readErr                –19        Driver does not respond to read requests
  268.         writErr                –20        Driver does not respond to write requests
  269.         badUnitErr            –21        Driver reference number does not
  270.                                     match unit table
  271.         unitEmptyErr        –22        Driver reference number specifies a
  272.                                     nil handle in unit table
  273.         abortErr            –27        Request aborted by KillIO
  274.         notOpenErr            –28        Driver not open
  275.         dskFulErr            -34        Destination volume is full
  276.         nsvErr                -35        No such volume
  277.         ioErr                -36        I/O error
  278.         bdNamErr            -37        Bad filename
  279.         tmfoErr                -42        Too many files open
  280.         fnfErr                -43        Source file not found, or destination
  281.                                     directory does not exist
  282.         wPrErr                -44        Volume locked by hardware
  283.         fLckdErr            -45        File is locked
  284.         vLckdErr             -46        Destination volume is read-only
  285.         fBsyErr                 -47        The source or destination file could
  286.                                     not be opened with the correct access
  287.                                     modes
  288.         dupFNErr            -48        Destination file already exists
  289.         opWrErr                -49        File already open for writing
  290.         paramErr            -50        No default volume or function not
  291.                                     supported by volume
  292.         permErr                 -54        File is already open and cannot be opened using specified deny modes
  293.         memFullErr            -108    Copy buffer could not be allocated
  294.         dirNFErr            -120    Directory not found or incomplete pathname
  295.         wrgVolTypErr        -123    Function not supported by volume
  296.         afpAccessDenied        -5000    User does not have the correct access
  297.         afpDenyConflict        -5006    The source or destination file could
  298.                                     not be opened with the correct access
  299.                                     modes
  300.         afpObjectTypeErr    -5025    Source is a directory, directory not found
  301.                                     or incomplete pathname
  302.     
  303.     __________
  304.     
  305.     Also see:    CopyErrProcPtr, CopyFilterProcPtr, FilteredDirectoryCopy,
  306.                 DirectoryCopy, FSpDirectoryCopy, FileCopy, FSpFileCopy
  307. */
  308.  
  309. /*****************************************************************************/
  310.  
  311. pascal    OSErr    DirectoryCopy(short srcVRefNum,
  312.                               long srcDirID,
  313.                               ConstStr255Param srcName,
  314.                               short dstVRefNum,
  315.                               long dstDirID,
  316.                               ConstStr255Param dstName,
  317.                               ConstStr255Param copyName,
  318.                               void *copyBufferPtr,
  319.                               long copyBufferSize,
  320.                               Boolean preflight,
  321.                               CopyErrProcPtr copyErrHandler);
  322. /*    ¶ Make a copy of a directory structure in a new location.
  323.     The DirectoryCopy function makes a copy of a directory structure in a
  324.     new location. If copyBufferPtr <> NIL, it points to a buffer of
  325.     copyBufferSize that is used to copy files data.  The larger the
  326.     supplied buffer, the faster the copy.  If copyBufferPtr = NIL, then this
  327.     routine allocates a buffer in the application heap. If you pass a
  328.     copy buffer to this routine, make its size a multiple of 512
  329.     ($200) bytes for optimum performance.
  330.     
  331.     DirectoryCopy normally creates a new directory *in* the specified
  332.     destination directory and copies the source directory's content into
  333.     the new directory. However, if root parent directory (fsRtParID)
  334.     is passed as the dstDirID parameter and NULL is passed as the
  335.     dstName parameter, DirectoryCopy renames the destination volume to
  336.     the source directory's name (truncating if the name is longer than
  337.     27 characters) and copies the source directory's content into the
  338.     destination volume's root directory. This special case is supported
  339.     by DirectoryCopy, but not by FSpDirectoryCopy since with
  340.     FSpDirectoryCopy, the dstName parameter can not be NULL.
  341.     
  342.     srcVRefNum        input:    Source volume specification.
  343.     srcDirID        input:    Source directory ID.
  344.     srcName            input:    Source directory name, or nil if
  345.                             srcDirID specifies the directory.
  346.     dstVRefNum        input:    Destination volume specification.
  347.     dstDirID        input:    Destination directory ID.
  348.     dstName            input:    Destination directory name, or nil if
  349.                             dstDirID specifies the directory.
  350.     copyName        input:    Points to the new directory name if the directory
  351.                             is to be renamed or nil if the directory isn't to
  352.                             be renamed.
  353.     copyBufferPtr    input:    Points to a buffer of copyBufferSize that
  354.                             is used the i/o buffer for the copy or
  355.                             nil if you want DirectoryCopy to allocate its
  356.                             own buffer in the application heap.
  357.     copyBufferSize    input:    The size of the buffer pointed to
  358.                             by copyBufferPtr.
  359.     preflight        input:    If true, DirectoryCopy makes sure there are
  360.                             enough allocation blocks on the destination
  361.                             volume to hold the directory's files before
  362.                             starting the copy.
  363.     copyErrHandler    input:    A pointer to the routine you want called if an
  364.                             error condition is detected during the copy, or
  365.                             nil if you don't want to handle error conditions.
  366.                             If you don't handle error conditions, the first
  367.                             error will cause the copy to quit and
  368.                             DirectoryCopy will return the error.
  369.                             Error handling is recommended...
  370.     
  371.     Result Codes
  372.         noErr                0        No error
  373.         readErr                –19        Driver does not respond to read requests
  374.         writErr                –20        Driver does not respond to write requests
  375.         badUnitErr            –21        Driver reference number does not
  376.                                     match unit table
  377.         unitEmptyErr        –22        Driver reference number specifies a
  378.                                     nil handle in unit table
  379.         abortErr            –27        Request aborted by KillIO
  380.         notOpenErr            –28        Driver not open
  381.         dskFulErr            -34        Destination volume is full
  382.         nsvErr                -35        No such volume
  383.         ioErr                -36        I/O error
  384.         bdNamErr            -37        Bad filename
  385.         tmfoErr                -42        Too many files open
  386.         fnfErr                -43        Source file not found, or destination
  387.                                     directory does not exist
  388.         wPrErr                -44        Volume locked by hardware
  389.         fLckdErr            -45        File is locked
  390.         vLckdErr             -46        Destination volume is read-only
  391.         fBsyErr                 -47        The source or destination file could
  392.                                     not be opened with the correct access
  393.                                     modes
  394.         dupFNErr            -48        Destination file already exists
  395.         opWrErr                -49        File already open for writing
  396.         paramErr            -50        No default volume or function not
  397.                                     supported by volume
  398.         permErr                 -54        File is already open and cannot be opened using specified deny modes
  399.         memFullErr            -108    Copy buffer could not be allocated
  400.         dirNFErr            -120    Directory not found or incomplete pathname
  401.         wrgVolTypErr        -123    Function not supported by volume
  402.         afpAccessDenied        -5000    User does not have the correct access
  403.         afpDenyConflict        -5006    The source or destination file could
  404.                                     not be opened with the correct access
  405.                                     modes
  406.         afpObjectTypeErr    -5025    Source is a directory, directory not found
  407.                                     or incomplete pathname
  408.     
  409.     __________
  410.     
  411.     Also see:    CopyErrProcPtr, FSpDirectoryCopy, FilteredDirectoryCopy,
  412.                 FSpFilteredDirectoryCopy, FileCopy, FSpFileCopy
  413. */
  414.  
  415. /*****************************************************************************/
  416.  
  417. pascal    OSErr    FSpDirectoryCopy(const FSSpec *srcSpec,
  418.                                  const FSSpec *dstSpec,
  419.                                  ConstStr255Param copyName,
  420.                                  void *copyBufferPtr,
  421.                                  long copyBufferSize,
  422.                                  Boolean preflight,
  423.                                  CopyErrProcPtr copyErrHandler);
  424. /*    ¶ Make a copy of a directory structure in a new location.
  425.     The FSpDirectoryCopy function makes a copy of a directory structure in a
  426.     new location. If copyBufferPtr <> NIL, it points to a buffer of
  427.     copyBufferSize that is used to copy files data.  The larger the
  428.     supplied buffer, the faster the copy.  If copyBufferPtr = NIL, then this
  429.     routine allocates a buffer in the application heap. If you pass a
  430.     copy buffer to this routine, make its size a multiple of 512
  431.     ($200) bytes for optimum performance.
  432.     
  433.     srcSpec            input:    An FSSpec record specifying the directory to copy.
  434.     dstSpec            input:    An FSSpec record specifying destination directory
  435.                             of the copy.
  436.     copyName        input:    Points to the new directory name if the directory
  437.                             is to be renamed or nil if the directory isn't to
  438.                             be renamed.
  439.     copyBufferPtr    input:    Points to a buffer of copyBufferSize that
  440.                             is used the i/o buffer for the copy or
  441.                             nil if you want DirectoryCopy to allocate its
  442.                             own buffer in the application heap.
  443.     copyBufferSize    input:    The size of the buffer pointed to
  444.                             by copyBufferPtr.
  445.     preflight        input:    If true, FSpDirectoryCopy makes sure there are
  446.                             enough allocation blocks on the destination
  447.                             volume to hold the directory's files before
  448.                             starting the copy.
  449.     copyErrHandler    input:    A pointer to the routine you want called if an
  450.                             error condition is detected during the copy, or
  451.                             nil if you don't want to handle error conditions.
  452.                             If you don't handle error conditions, the first
  453.                             error will cause the copy to quit and
  454.                             DirectoryCopy will return the error.
  455.                             Error handling is recommended...
  456.     
  457.     Result Codes
  458.         noErr                0        No error
  459.         readErr                –19        Driver does not respond to read requests
  460.         writErr                –20        Driver does not respond to write requests
  461.         badUnitErr            –21        Driver reference number does not
  462.                                     match unit table
  463.         unitEmptyErr        –22        Driver reference number specifies a
  464.                                     nil handle in unit table
  465.         abortErr            –27        Request aborted by KillIO
  466.         notOpenErr            –28        Driver not open
  467.         dskFulErr            -34        Destination volume is full
  468.         nsvErr                -35        No such volume
  469.         ioErr                -36        I/O error
  470.         bdNamErr            -37        Bad filename
  471.         tmfoErr                -42        Too many files open
  472.         fnfErr                -43        Source file not found, or destination
  473.                                     directory does not exist
  474.         wPrErr                -44        Volume locked by hardware
  475.         fLckdErr            -45        File is locked
  476.         vLckdErr             -46        Destination volume is read-only
  477.         fBsyErr                 -47        The source or destination file could
  478.                                     not be opened with the correct access
  479.                                     modes
  480.         dupFNErr            -48        Destination file already exists
  481.         opWrErr                -49        File already open for writing
  482.         paramErr            -50        No default volume or function not
  483.                                     supported by volume
  484.         permErr                 -54        File is already open and cannot be opened using specified deny modes
  485.         memFullErr            -108    Copy buffer could not be allocated
  486.         dirNFErr            -120    Directory not found or incomplete pathname
  487.         wrgVolTypErr        -123    Function not supported by volume
  488.         afpAccessDenied        -5000    User does not have the correct access
  489.         afpDenyConflict        -5006    The source or destination file could
  490.                                     not be opened with the correct access
  491.                                     modes
  492.         afpObjectTypeErr    -5025    Source is a directory, directory not found
  493.                                     or incomplete pathname
  494.     
  495.     __________
  496.     
  497.     Also see:    CopyErrProcPtr, DirectoryCopy, FilteredDirectoryCopy,
  498.                 FSpFilteredDirectoryCopy, FileCopy, FSpFileCopy
  499. */
  500.  
  501. /*****************************************************************************/
  502.  
  503. #ifdef __cplusplus
  504. }
  505. #endif
  506.  
  507. #include "OptimizationEnd.h"
  508.  
  509. #endif    /* __DIRECTORYCOPY__ */
  510.